#ifndef _UTILS_H_
#define _UTILS_H_

#include <string>
#include "Define.h"

std::string GetStrTime(u64 t);
std::string GetNowTime();
time_t Convert2Time(const char *strtime);
std::string GetCurMonth();
bool IsSameDay(u64 t1, u64 t2);
s32 GetCurDay();
std::string ConvertIp(u32 ip);
s32 SecPassFromDay(u64 now);
s32 GetCurHour(u64 now);

#define APPEND_INT8(s, d)	s.append((char*)&d, sizeof(char));
#define APPEND_INT16(s, d)	s.append((char*)&d, sizeof(short));
#define APPEND_INT32(s, d)	s.append((char*)&d, sizeof(int));
#define APPEND_INT64(s, d)	s.append((char*)&d, sizeof(long long));
#define APPEND_STR(s, str)	s.append((char*)str);
#define APPEND_STR_LEN(s, str, len)	s.append((char*)str, len);

#define TIME_NOW    (u64)time(NULL) 

#define CHECK_PARSE_MSG(_msg, _data, _len) \
if( _msg.ParseFromArray((const char*)_data, _len) == IMessage::E_Error )\
{\
    LOG_ERROR( "[%s][%s][%d] msg error, len[%d]", __FILE__, __FUNCTION__, __LINE__, _len );\
    return;\
}

#define CHECK_PARSE_MSG_EXPR(_msg, _data, _len, _expr) \
if( _msg.ParseFromArray((const char*)_data, _len) == IMessage::E_Error )\
{\
    LOG_ERROR( "[%s][%s][%d] msg error, len[%d]", __FILE__, __FUNCTION__, __LINE__, _len );\
    _expr;\
}

#define CHECK_PARSE_MSG_EXPR2(_msg, _expr) \
if(1) \
{\
    LOG_ERROR( "[%s][%s][%d] msg error", __FILE__, __FUNCTION__, __LINE__);\
    _expr;\
}

#define CHECK_SERIALIZE_MSG_EX(_msgid, _msg, _parser, _buf, _buflen, _outlen, _expr) {\
    s32 _headlen = _parser->GetHeadLen();\
	s32 _msglen = _msg.SerializeToArray((char*)_buf+_headlen, _buflen-_headlen);\
	if(_msglen == IMessage::E_Error)\
    {\
        LOG_ERROR( "[%s][%s][%d] SerializeToArray msg[%u] error", __FILE__, __FUNCTION__, __LINE__, _msgid );\
		_expr;\
    }\
	_outlen = _msglen + _headlen;\
	_parser->FillHeader( (byte*)_buf, _outlen, _msgid );\
}


#define CHECK_SERIALIZE_MSG(_msgid, _msg, _parser, _expr) \
static char msgbuffer[CIoSocketEx::E_MaxPkg_Len];\
s32 msglen = 0;\
CHECK_SERIALIZE_MSG_EX(_msgid, _msg, _parser, msgbuffer, sizeof(msgbuffer), msglen, _expr)

#define PackBackData(_bdata, _userid, _gateid, _clientid) do{\
    publicproto::BackData _bd;\
    _bd.userid = _userid;\
    _bd.gateid = _gateid;\
    _bd.clientid = _clientid;\
    char _tmp[128] = {0};\
    int _len = _bd.SerializeToArray(_tmp, sizeof(_tmp));\
    _bdata.append( _tmp, _len );\
}while(0);

extern const s32 BASICINFO_BATCH_LEN;

#endif

